from pyb import UART
import ustruct

uart3 = UART(3, 115200, timeout_char=1000)

while(True):
    if uart3.any():
        dat = uart3.read()
        if dat==b'\x00':continue
        dat = ustruct.unpack('<bbbbbbbbbbbbbbbbbbbb',dat)
        print(dat)

def write_data(data):
    if len(data) > 0:
        cmd = [0x56,0xAB,0x14,0xE8,0x13,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xCF]
        for i in range(0, len(data)):
            cmd[i+5] = data[i]
        print(cmd)
        uart3.write(ustruct.pack("<bbbbbbbbbbbbbbbbbbbb",cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5],cmd[6],cmd[7],cmd[8],cmd[9],cmd[10],cmd[11],cmd[12],cmd[13],cmd[14],cmd[15],cmd[16],cmd[17],cmd[18],cmd[19]))

def read_data():
    cmd = 0
    data = 0
    if uart3.any():
        buf = bytearray(20)
        uart3.readinto(buf)
        cmd = buf[5]
        data = buf[6]